Skip to content

Fix NFA compilation failure for grammars with shared optional prefixes#2629

Merged
GeorgeNgMsft merged 40 commits into
mainfrom
dev/georgeng/fix_nfa_with_subrules
Jul 14, 2026
Merged

Fix NFA compilation failure for grammars with shared optional prefixes#2629
GeorgeNgMsft merged 40 commits into
mainfrom
dev/georgeng/fix_nfa_with_subrules

Conversation

@GeorgeNgMsft

@GeorgeNgMsft GeorgeNgMsft commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

See issue #2499

Problem

The NFA compiler rejected grammars where multiple top-level alternatives share
a common optional prefix (e.g.   = | ; =

foo -> ...;

= (please)?; ). The optimizer's  factorCommonPrefixes  pass can produce a
value-less, multi-term rule in this shape, which the NFA compiler had no
general way to derive a value for — it only special-cased a narrower "single
wildcard/number part" shape.

Root-cause fix

•  nfaCompiler.ts  now derives a rule's effective value (explicit  -> 
expression, or implicit single-variable-part forwarding) using the same
general logic the AST-based optimizer already used, instead of its old,
narrower special case.
• Extracted this rule-value-derivation logic into a new shared module,
 grammarValueDeriver.ts  — a single source of truth now used by both the
grammar optimizer and the NFA compiler (previously each had its own
slightly different implementation).
• Side effect of consolidation: nested rules with a single bound variable and
no explicit value now correctly forward that value in the NFA path instead
of silently discarding it.
• Harmonized error messages for rules with no value and no single
variable-bearing part to forward, across both the "ambiguous" and
"missing" throw paths.

Follow-up hardening

• Hoisted the separate  tailCall  (NYI tail-RulesPart) rejection check to a
single, earliest, unconditional location ( normalizeRule ), run before any
rule rewriting — closing a gap where one rule shape (a single-part
tailCall rule) could slip past the check and surface a confusing,
differently-worded error later in compilation.
• Unified the optimizer's  getImplicitDefaultValue  onto the same shared
 deriveEffectiveValue , removing a second, redundant implementation.

Testing

• Added targeted unit tests for the shared derivation helper and regression
tests for previously-unreachable error paths (0-part / 1-part rules with
no value, and the single-part tailCall shape).
• Cleaned up stale comments/tests referencing removed or relocated
functions; verified no redundant test coverage was introduced.
• Full suite: 75/75 suites, 16153/16155 tests passing (2 skipped), clean
build, prettier clean.

@GeorgeNgMsft GeorgeNgMsft marked this pull request as ready for review July 8, 2026 23:56
Copilot AI review requested due to automatic review settings July 8, 2026 23:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an @typeagent/action-grammar NFA compilation failure triggered by optimizer output for grammars whose top-level alternatives share a fully-optional common prefix (e.g. optional “please”), and aligns the agc default optimization preset with NFA compatibility (agent-server’s default grammar system).

Changes:

  • Teach the NFA compiler to derive an implicit forwarding value for value-less multi-part rules when exactly one part is variable-bearing (and error clearly when ambiguous).
  • Add an nfaSafeOptimizations preset (recommended minus tailFactoring / promoteTailRulesParts) and switch agc’s default optimizations to that preset.
  • Add regression tests covering the repro, the still-unsupported tailCall path, and the ambiguous implicit-value guard.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
ts/packages/actionGrammarCompiler/src/commands/compile.ts Switch agc default optimizations to nfaSafeOptimizations for NFA compatibility.
ts/packages/actionGrammar/test/nfaFactoredPrefixValues.spec.ts New regression tests for shared optional-prefix factoring, tailCall refusal, and ambiguous implicit value.
ts/packages/actionGrammar/src/nfaCompiler.ts Derive implicit values for value-less multi-part rules with a single variable-bearing part; improve diagnostics.
ts/packages/actionGrammar/src/index.ts Export nfaSafeOptimizations from the package entrypoint.
ts/packages/actionGrammar/src/grammarOptimizer.ts Introduce the nfaSafeOptimizations preset and document why it exists.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ts/packages/actionGrammarCompiler/src/commands/compile.ts Outdated
Comment thread ts/packages/actionGrammar/test/nfaFactoredPrefixValues.spec.ts
Line-number shifts from the NFA fix broke file:line matches for
pre-existing baselined offenders (nfaCompiler.ts, grammarOptimizer.ts).
Regenerated via 'npm run code-complexity:update-exceptions'.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 9, 2026 00:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread ts/packages/actionGrammar/test/nfaFactoredPrefixValues.spec.ts Outdated
Comment thread ts/packages/actionGrammar/src/nfaCompiler.ts Outdated
Comment thread ts/packages/actionGrammar/test/nfaFactoredPrefixValues.spec.ts Outdated
Comment thread ts/packages/actionGrammarCompiler/src/commands/compile.ts Outdated
Comment thread ts/packages/actionGrammar/src/nfaCompiler.ts Outdated
Comment thread ts/packages/actionGrammar/src/nfaCompiler.ts Outdated
Comment thread ts/packages/actionGrammar/src/grammarValueDeriver.ts Outdated
Comment thread ts/packages/actionGrammar/src/grammarOptimizer.ts Outdated
Comment thread ts/packages/actionGrammar/src/nfaCompiler.ts Outdated
Comment thread ts/packages/actionGrammar/src/grammarOptimizer.ts Outdated
Comment thread ts/packages/actionGrammar/src/grammarValueTypeValidator.ts Outdated
@GeorgeNgMsft GeorgeNgMsft enabled auto-merge July 14, 2026 01:46
@GeorgeNgMsft GeorgeNgMsft added this pull request to the merge queue Jul 14, 2026
Merged via the queue into main with commit c1df1ba Jul 14, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants